try {
	Type.registerNamespace('PoArriveUtil');

	PoArriveUtil.batchInsertTableVOs = function(VOClass, vos) {
		if(NCMMPubUtil.isEmpty(vos)) {
			return;
		}
		var attrs = VOClass.prototype.getAllAttrs();

		var fields = attrs.map(function(row, i, arr) {
			return row.name;
		});
		//$alert("fields=" + $jsonToString(fields));
		var valueMapList = Array.prototype.map.call(vos, function(row, i, arr) {
			var billinfo_local = $stringToJSON((row || {}));
			var values = attrs.map(function(attr, i, array) {
				var fname = attr.name;
				if(!!billinfo_local[fname]) {
					if(billinfo_local[fname] != "" && typeof(billinfo_local[fname]) == "string") {
						billinfo_local[fname] = NCMMPubUtil.htmlspecialchars2sqliteex(billinfo_local[fname]);
					}
					return billinfo_local[fname];
				} else {
					if(attr.type == NCMMPubUtil.VALUE_TYPE_INTEGER) {
						return 0;
					} else if(attr.type == NCMMPubUtil.VALUE_TYPE_REAL) {
						return 0.0;
					} else {
						return "";
					}
				}
			});
			//$alert("values="+$jsonToString(values))
			return values;
		});

		var sum = valueMapList.length;
		//$alert("valueMapList=" + $jsonToString(valueMapList));

		$app.replaceBatch({
			db: NCMMPubUtil.DatabaseName,
			table: VOClass.prototype.getTableName(),
			fields: fields,
			values: valueMapList,
			callback: function(args_null, args) {}
		});
		return sum;

	};
	/**
	 * 批量跟新数据
	 * @param {Object} VOClass 类
	 * @param {Object} vos 待更新vo数组
	 * @param {Object} unUpdateFieldanme 表中那一组列名不更新，字符串数组数组
	 */
	PoArriveUtil.updateBatchVOs = function(VOClass, vos, scanedname) {
		if(NCMMPubUtil.isEmpty(vos)) {
			return;
		}
		if(NCMMPubUtil.isEmpty(vos)) {
			return;
		}
		
		for(var i = 0; i < vos.length; i++) {
			NCMMPubUtil.updateVO(PoArriveVO, vos[i]);
		}
		/**var attrs = VOClass.prototype.getAllAttrs();
		var fields = [];
		for(var i = 0; i < attrs.length; i++) {
			if(attrs[i].name != scanedname) {
				fields.push(attrs[i].name);
			}
		}
		//$alert("fields="+$jsonToString({fields:fields}));
		var values = [];
		for(var j = 0; j < vos.length; j++) {
			var row = $stringToJSON((vos[j] || {}));
			var rowVaules = [];
			for(var k = 0; k < attrs.length; k++) {
				var fname = attrs[k].name;
				var type = attrs[k].type;
				if(fields != scanedname) {
					if(!!row[fname]) {
						if(row[fname] != "" && typeof(row[fname]) == "string") {
							row[fname] = NCMMPubUtil.htmlspecialchars2sqliteex(row[fname]);
						}
						rowVaules.push(row[fname]);
					} else {
						if(type == NCMMPubUtil.VALUE_TYPE_INTEGER) {
							rowVaules.push(0);
						} else if(type == NCMMPubUtil.VALUE_TYPE_REAL) {
							rowVaules.push(0.0);

						} else {
							rowVaules.push("");
						}
					}
				}
			}
			values.push(rowVaules);

		}
		var sum = rowVaules.length;
		//$alert("rowVaules=" + $jsonToString(values));
		$app.replaceBatch({
			db: NCMMPubUtil.DatabaseName,
			table: VOClass.prototype.getTableName(),
			fields: fields,
			values: values,
			callback: function(args_null, args) {}
		});
		return sum;
**/
	};
	PoArriveUtil.updateScans = function(vos) {
		if(NCMMPubUtil.isEmpty(vos)) {
			return;
		}
		
		for(var i = 0; i < vos.length; i++) {
			updateScanTable(vos[i]);
		}
	};
		function updateScanTable(billdata) {
		var wheresql = " where pk_order_b='" + billdata.pk_order_b + "' ";
		NCMMPubUtil.queryVO(PoArriveScanVO, wheresql, function(data) {
			if(data != null) {
				var datas = eval(data);
				datas = NCMMPubUtil.htmlspecialjsonarray2UI(datas);
				//datas = NCMMPubUtil.htmlspecialjson2UI(datas);
				for(var i = 0; i < datas.length; i++) {
					datas[i]["hts"] = billdata.hts;
					datas[i]["bts"] = billdata.bts;
					datas[i]["nnum"] = billdata.nnum;
					datas[i]["nastnum"] = billdata.nastnum;
					NCMMPubUtil.updateVO(PoArriveScanVO, datas[i]);
				}
			}
		});
	};
	PoArriveUtil.getScanVOCount = function(pk_order_b) {
	    var len=0;		
		var sql = " where pk_order_b = '"+pk_order_b+"'";
		NCMMPubUtil.queryVO(PoArriveScanVO, sql, function(scanData) {
			if(typeof(scanData) == 'undefined' || scanData == null || scanData.length == 0) {
				return;
			}
			var data = eval(scanData);	
			len= data.length;
		});
		return len;
	};
	PoArriveUtil.getUnitBitnumber = function(unitid) {
		//更新发货单入库列表增加扫描数量 
		var wheresql = "where pk_measdoc='" + unitid + "'";
		var bitnumber = 0;
		if(!NCMMPubUtil.isTableExist(MeasdocVO)) {
			return;
		}
		NCMMPubUtil.queryVO(MeasdocVO, wheresql, function(data) {
			if(typeof(data) == 'undefined' || data == null || data.length == 0) {
				return;
			}
			var unitData = eval(data);
			if(unitData.length > 0) {
				bitnumber = unitData[0].bitnumber;
			} else {
				return;
			}
		});
		return bitnumber;
	};
	PoArriveUtil.getScanedNum=function(pk_order_b){
	   var sql = " where pk_order_b = '"+pk_order_b+"'";
	   var num={};
	   NCMMPubUtil.queryVO(PoArriveVO, sql, function(data) {
			if(typeof(data) == 'undefined' || data == null || data.length == 0) {
		    	return;
			}
			var vos = eval(data);
			vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
			if(vos == null || vos.length == 0) {
				return;
			}			
				//$alert("从数据库读取的数据vo[0]的json格式为：" + $jsonToString(vos[0]));	
			num["nnum"]=parseFloat(vos[0].nnum);
			if(NCMMPubUtil.isNotEmpty(vos[0].nscanednum)){	
			num["nscanednum"] = parseFloat(vos[0].nscanednum);	
			}
			else{
			num["nscanednum"] = 0;
			}	
		});
		return num;
	};
	PoArriveUtil.updateScanedNum = function(pk_order_b) {
		//更新发货单入库列表增加扫描数量 
		var sql = " where pk_order_b = '"+pk_order_b+"'";
		var num = 0;
		var astnum = 0;
		NCMMPubUtil.queryVO(PoArriveScanVO, sql, function(scanData) {
			if(NCMMPubUtil.isNotEmpty(scanData)) {
				var scanvos = eval(scanData);

				for(var i = 0; i < scanvos.length; i++) {
					num = getAddScanedNum(num, scanvos[i].nscannum);
					astnum = getAddScanedNum(astnum, scanvos[i].nscanastnum);
				}
			}
			NCMMPubUtil.queryVO(PoArriveVO, sql, function(data) {
				if(typeof(data) == 'undefined' || data == null || data.length == 0) {
					return;
				}
				var vos = eval(data);
				vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
				if(vos == null || vos.length == 0) {
					return;
				}
				//$alert("从数据库读取的数据vo[0]的json格式为：" + $jsonToString(vos[0]));		
				vos[0]["nscanednum"] = num;
				vos[0]["nscanedastnum"] = astnum;
				//3.2 更新发货单表已扫描主数量
				//$alert("更新数据库的vo[0]的json格式为：" + $jsonToString(vos[0]));
				NCMMPubUtil.updateVO(PoArriveVO, vos[0]);
			});
		});
	};
	PoArriveUtil.getAddNum = function(num1, num2) {
		if(num1 == null || num1 == "") {
			num1 += "00.00000";
		} else {
			num1 = parseFloat(num1);
		}
		if(num2 == null || num2 == "") {
			num2 += "1.00000";
		} else {
			num2 = parseFloat(num2);
		}
		var newNum = parseFloat(num1).add(parseFloat(num2));
		return newNum;
	};

	function getAddScanedNum(scanedNum, currentScanedNum) {
		if(scanedNum == null || scanedNum == "") {
			scanedNum += "00.00000";
		} else {
			scanedNum = parseFloat(scanedNum);
		}
		if(currentScanedNum == null || currentScanedNum == "") {
			currentScanedNum += "1.00000";
		} else {
			currentScanedNum = parseFloat(currentScanedNum);
		}
		var newScanedNum = parseFloat(scanedNum).add(parseFloat(currentScanedNum));
		return newScanedNum;
	};
	PoArriveUtil.deleteVOsAfterCommit = function(billbids) {
		var sql = " pk_order_b in ('0'";
		for(var i = 0; i < billbids.length; i++) {
			sql = sql + ",'" + billbids[i] + "'";
		}
		sql = sql + ")";
		deleteVOByWhere(PoArriveScanVO, sql);
		deleteVOByWhere(PoArriveVO, sql);
	};
	PoArriveUtil.checkWhenCommit = function(itemKey, scanCount, scanNum) {
		var sql = " where pk_order_b in ('0'";
		for(var i = 0; i < itemKey.length; i++) {
			sql = sql + ",'" + itemKey[i] + "'";
		}
		sql = sql + ")";
		var checkResult = "";
		NCMMPubUtil.queryVO(PoArriveScanVO, sql, function(scanData) {
			if(typeof(scanData) == 'undefined' || scanData == null || scanData.length == 0) {
				return;
			}
			var countNum = {};
			var vos = {};
			var data = eval(scanData);
			for(var i = 0; i < data.length; i++) {
				if(NCMMPubUtil.isEmpty(countNum) || NCMMPubUtil.isEmpty(countNum[data[i].pk_order_b])) {
					countNum[data[i].pk_order_b] = 1;
					vos[data[i].pk_order_b] = data[i];
				} else {
					countNum[data[i].pk_order_b] = countNum[data[i].pk_order_b] + 1;
				}
			}
			for(var i = 0; i < itemKey.length; i++) {
				var vo = vos[itemKey[i]];
				if(countNum[itemKey[i]] != scanCount[itemKey[i]]) {
					checkResult = "单据号" + vo.vbillcode + "物料" + vo.cmaterialvid_name + "必须选中其所有扫描记录才能提交！";
				}
			}
		});
		return checkResult;
	};

	function deleteVOByWhere(VOClass, whereSql) {
		var tblname = VOClass.prototype.getTableName();
		var sql = "delete from " + tblname + " where " + whereSql;
		var param = {
			db: NCMMPubUtil.DatabaseName,
			sql: sql
		}
		$sqlite.execSql(param);
	};
	PoArriveUtil.isBsBitnumber = function(num, unitid) {
		var unitBitnum = PoArriveUtil.getUnitBitnumber(unitid);
		if(NCMMPubUtil.isEmpty(unitBitnum)) {
			return false;
		}
		if(num.split(".").length == 1) {
			return false;
		}
		var numBitNum = num.split(".")[1].length;
		if(numBitNum > unitBitnum) {
			return true;
		}
		return false;
	};
	/**
	 *计算主数量 
	 */
	PoArriveUtil.getNum = function(nastnum, vchange) {
	    var vchange1=1;//分子
	    var vchange2=1;//分母
		if(nastnum == null || nastnum == "") {
			nastnum += "00.00000";
		} else {
			nastnum = parseFloat(nastnum);
		}
		if(vchange == null || vchange == "") {
			vchange += "1.00000";
		} else {
		    vchange1=parseFloat(vchange.split("/")[0]);
		    vchange2=parseFloat(vchange.split("/")[1]);
		}
		//主数量=辅数量*换算率=辅数量*分子/分母
		var num = nastnum.mul(vchange1).div(vchange2);
		return num;
	};
	/**
	 *主数量除以换算率得辅数量 
	 * @param {Object} nnum
	 * @param {Object} vchange
	 */
	PoArriveUtil.getAstnum = function(nnum, vchange) {
	    var vchange1=1;//分子
	    var vchange2=1;//分母
		if(nnum == null || nnum == "") {
			nnum += "00.00000";
		} else {
			nnum = parseFloat(nnum);
		}
		if(vchange == null || vchange == "") {
			vchange += "1.00000";
		} else {
			vchange1=parseFloat(vchange.split("/")[0]);
		    vchange2=parseFloat(vchange.split("/")[1]);
		}
		//辅数量=主数量/换算率=主数量*分母/分子
		var nastum = nnum.mul(vchange2).div(vchange1);
		return nastum;
	};
	PoArriveUtil.openDropDownList = function(controlid) {
		$menu.openDropDownList({
			"controlid": controlid, //目标控件的id
			"dropDownListWidth": "100", //菜单项的宽度
			"background": "#f2f2f2",
			//"background-image":"app.png",
			"color": "#ee4311",
			"font-size": "12",
			"halign": "center", //right  center
			"split-color": "#ee4311",
			//"margin-left":"10",
			"panelstyle": "round-div",
			"border-color": "#ee4311",
			//"showtype":"right",
			"dropItemsArray": [{
				"id": "id_homepage",
				"name": "主菜单", //菜单项名称
				"action": "gotohomepage()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_datamanage",
				"name": "数据管理", //菜单项名称
				"action": "gotodatamanage()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_setting",
				"name": "业务设置", //菜单项名称
				"action": "setting()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_logout",
				"name": "注销", //菜单项名称
				"action": "logout()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_scanlist",
				"name": "扫描记录", //菜单项名称
				"action": "gotoscanlist()" //点击该菜单项时执行的JS方法
			}]
		});
	};
	PoArriveUtil.openDropDownListForScan = function(controlid) {
		$menu.openDropDownList({
			"controlid": controlid, //目标控件的id
			"dropDownListWidth": "100", //菜单项的宽度
			"background": "#f2f2f2",
			//"background-image":"app.png",
			"color": "#ee4311",
			"font-size": "12",
			"halign": "center", //right  center
			"split-color": "#ee4311",
			//"margin-left":"10",
			"panelstyle": "round-div",
			"border-color": "#ee4311",
			//"showtype":"right",
			"dropItemsArray": [{
				"id": "id_homepage",
				"name": "主菜单", //菜单项名称
				"action": "gotohomepage()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_datamanage",
				"name": "数据管理", //菜单项名称
				"action": "gotodatamanage()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_setting",
				"name": "业务设置", //菜单项名称
				"action": "setting()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_logout",
				"name": "注销", //菜单项名称
				"action": "logout()" //点击该菜单项时执行的JS方法
			}, {
				"id": "id_scanlist",
				"name": "扫描记录", //菜单项名称
				"action": "gotoscanlistForScan()" //点击该菜单项时执行的JS方法
			}]
		});
	};

	function gotohomepage() {
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.HomePage", //目标页面（首字母大写）全名
			"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
		});
	}

	function gotodatamanage() {
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.DM_main", //目标页面（首字母大写）全名
			"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
		});
	}

	function setting() {
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.BusinessSettingPage", //目标页面（首字母大写）全名
			"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
		});
	}

	function logout() {
		//$view.close();
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.LoginPage", //目标页面（首字母大写）全名
			"isKeep": "false", //打开新页面的同时是否保留当前页面，true为保留， false为不保留
		});
	}

	function gotoscanlist() {
		//$view.close();
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.PoArriveScanList", //目标页面（首字母大写）全名
			"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留， false为不保留
		});
	}

	function gotoscanlistForScan() {
		//$view.close();
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.PoArriveScanList", //目标页面（首字母大写）全名
			"isKeep": "false", //打开新页面的同时是否保留当前页面，true为保留， false为不保留
		});
	}
} catch(e) {
	$e(e);
}